﻿<template>
	<el-dialog
		:title="titleMap[mode]"
		v-model="visible"
		:width="1000"
		destroy-on-close
		@closed="$emit('closed')"
	>
		<el-form
			:model="entityObj"
			:rules="rules"
			:disabled="mode == 'Show'"
			ref="dialogForm"
			label-width="100px"
		>
			<el-row
				><el-col :span="12"
					><el-form-item
						label-width="120px"
						label="机构"
						prop="OrgName"
						><el-input
							v-model="entityObj.OrgName"
							disabled /></el-form-item></el-col
				><el-col :span="12"
					><el-form-item label-width="120px" label="帐号" prop="Code"
						><el-input
							v-model="entityObj.Code"
							disabled /></el-form-item></el-col
			></el-row>
			<el-row
				><el-col :span="12"
					><el-form-item label-width="120px" label="姓名" prop="Name"
						><el-input
							v-model="entityObj.Name" /></el-form-item></el-col
				><el-col :span="12"
					><el-form-item
						label-width="120px"
						label="部门"
						prop="DepartCode"
						><el-select
							v-model="entityObj.DepartCode"
							placeholder="---请选择---"
							style="width: 100%"
						>
							<el-option
								v-for="item in optionsDepartCode"
								:key="item.value"
								:label="item.label"
								:value="item.value"
							/> </el-select></el-form-item></el-col></el-row
			><el-row
				><el-col :span="12"
					><el-form-item
						label-width="120px"
						label="性别"
						prop="SexCode"
						><el-select
							v-model="entityObj.SexCode"
							style="width: 100%"
							placeholder="---请选择---"
						>
							<el-option
								v-for="item in optionsSexCode"
								:key="item.value"
								:label="item.label"
								:value="item.value"
							/> </el-select></el-form-item></el-col
				><el-col :span="12"
					><el-form-item
						label-width="120px"
						label="职务"
						prop="DutyCode"
						><el-select
							v-model="entityObj.DutyCode"
							style="width: 100%"
							placeholder="---请选择---"
						>
							<el-option
								v-for="item in optionsDutyCode"
								:key="item.value"
								:label="item.label"
								:value="item.value"
							/> </el-select></el-form-item></el-col></el-row
			><el-row
				><el-col :span="12"
					><el-form-item
						label-width="120px"
						label="手机"
						prop="Mobile"
						><el-input
							v-model="entityObj.Mobile" /></el-form-item></el-col
				><el-col :span="12"
					><el-form-item
						label-width="120px"
						label="Email"
						prop="Email"
						><el-input
							v-model="
								entityObj.Email
							" /></el-form-item></el-col></el-row
			><el-row
				><el-col :span="12"
					><el-form-item
						label-width="120px"
						label="微信号"
						prop="WeChat"
						><el-input
							v-model="entityObj.WeChat" /></el-form-item></el-col
				><el-col :span="12"
					><el-form-item
						label-width="120px"
						label="出生年月"
						prop="BirthDate"
						><el-date-picker
							style="width: 100%"
							v-model="entityObj.BirthDate"
							value-format="YYYY-MM-DD"
							placeholder="请输入出生年月" /></el-form-item></el-col></el-row
			><el-row
				><el-col :span="12"
					><el-form-item
						label-width="120px"
						label="身份证号"
						prop="IdNumber"
						><el-input
							v-model="
								entityObj.IdNumber
							" /></el-form-item></el-col></el-row
			><el-row
				><el-col :span="12"
					><el-form-item
						label-width="120px"
						label="客户经理"
						prop="IsCustomerMan"
						><el-switch
							v-model="entityObj.IsCustomerMan"
							active-value="1"
							inactive-value="0" /></el-form-item></el-col
				><el-col :span="12"
					><el-form-item
						label-width="120px"
						label="可点单"
						prop="AllowOrder"
						><el-switch
							v-model="entityObj.AllowOrder"
							active-value="1"
							inactive-value="0" /></el-form-item></el-col></el-row
			><el-form-item label-width="120px" label="说明"
				><el-input
					v-model="entityObj.Remark"
					clearable
					type="textarea" /></el-form-item
			><el-form-item label-width="120px" label="附件" prop="attachment"
				><sc-upload-multiple
					v-model="entityObj.Attachment"
					:objName="'UserInfo'"
					draggable
					:limit="8"
					tip="最多上传8个文件,请上传图像格式文件" /></el-form-item
			><el-row
				><el-col :span="12"
					><el-form-item label-width="120px" label="状态"
						><el-switch
							v-model="entityObj.State"
							active-value="1"
							inactive-value="0" /></el-form-item></el-col
			></el-row>
		</el-form>
		<template #footer>
			<el-input-number
				v-if="mode == 'Add' && addNum > 1"
				style="width: 120px"
				min="1"
				max="100"
				step="1"
				step-strictly="true"
				v-model="addNum"
			/>
			&nbsp;
			<el-button @click="visible = false">取 消</el-button>
			<el-button
				v-if="mode != 'show'"
				type="primary"
				:loading="isSaveing"
				@click="submit()"
				>保 存</el-button
			>
		</template>
	</el-dialog>
</template>

<script>
import publicjs from "@/api/model/publicjs";
export default {
	emits: ["success", "closed"],
	data() {
		return {
			props1: {
				checkStrictly: true,
				emitPath: false,
			},
			mode: "Add",
			titleMap: {
				Add: "新增",
				Edit: "编辑",
				Show: "查看",
			},
			addNum: 1,
			visible: false,
			isSaveing: false,
			needSaveFlag: false,
			keepList: ["Code"],
			fieldList: [
				"Code",
				"Name",
				"OrgCode",
				// "BrandCode",
				"DepartCode",
				"SexCode",
				"DutyCode",
				"Mobile",
				"Email",
				"WeChat",
				"BirthDate",
				"IdNumber",
				"IsCustomerMan",
				"AllowOrder",
				"Remark",
				"Attachment",
				"State",
				"InLocal",
			],
			//表单数据
			entityObj: {
				Code: "",
				Code_Old: "",
				Name: "",
				Name_Old: "",
				OrgCode: "",
				OrgCode_Old: "",
				// BrandCode: "",
				// BrandCode_Old: "",
				OrgName: "",
				OrgName_Old: "",
				DepartCode: "",
				DepartCode_Old: "",
				SexCode: "",
				SexCode_Old: "",
				DutyCode: "",
				DutyCode_Old: "",
				Mobile: "",
				Mobile_Old: "",
				Email: "",
				Email_Old: "",
				WeChat: "",
				WeChat_Old: "",
				BirthDate: "",
				BirthDate_Old: "",
				IdNumber: "",
				IdNumber_Old: "",
				IsCustomerMan: "",
				IsCustomerMan_Old: "",
				AllowOrder: "",
				AllowOrder_Old: "",
				Remark: "",
				Remark_Old: "",
				Attachment: "",
				Attachment_Old: "",
				State: "1",
				State_Old: "1",
				InLocal: "0",
				InLocal_Old: "",
			},
			//验证规则
			rules: {
				Name: [{ required: true, type: "string", message: "请填写" }],
				DepartCode: [{ required: true }],
				Attachment: [{ type: "string" }],
			},
			optionsOrgCode: [],
			optionsDepartCode: [],
			optionsSexCode: [],
			optionsDutyCode: [],
			// optionsBrandCode: [],
		};
	},
	methods: {
		//显示=
		open(mode = "Add", orgCode = "", orgName = "") {
			this.mode = mode;
			this.visible = true;
			if (orgCode != "") {
				this.entityObj.OrgCode = orgCode;
				this.entityObj.OrgCode_Old = orgCode;
			}
			if (orgName != "") {
				this.entityObj.OrgName = orgName;
				this.entityObj.OrgName_Old = orgName;
			}
			return this;
		},
		getChangeValue() {
			// eslint-disable-next-line no-debugger
			//debugger;
			let newObj = {};
			if (this.mode == "Add") {
				this.needSaveFlag = true;
				this.fieldList.forEach((element) => {
					//if (element == "OrgCode") {
						// this.optionsBrandCode.forEach((item) => {
						// 	if (item.value == this.entityObj[element]) {
						// 		newObj[element] = "0001";
						// 	}
						// });
					//} else {
						newObj[element] = this.entityObj[element];
					//}
				});
			} else {
				this.fieldList.forEach((element) => {
					if (
						this.entityObj[element] !=
						this.entityObj[element + "_Old"]
					) {
						newObj[element] = this.entityObj[element];
						newObj[element + "_Old"] =
							this.entityObj[element + "_Old"];
						this.needSaveFlag = true;
					}
				});
				if (this.needSaveFlag) {
					this.keepList.forEach((element) => {
						newObj[element] = this.entityObj[element];
						newObj[element + "_Old"] =
							this.entityObj[element + "_Old"];
					});
				}
			}
			// eslint-disable-next-line no-debugger
			//debugger;
			return newObj;
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				// eslint-disable-next-line no-debugger
				//debugger;
				if (valid) {
					this.isSaveing = true;
					// eslint-disable-next-line no-debugger
					//debugger;
					var changeList = this.getChangeValue();
					if (!this.needSaveFlag) {
						alert("没有做修改，无需保存");
						this.isSaveing = false;
						return;
					}
					//得到的数据this.entityObj
					var head = new publicjs.head();
					publicjs.initHead(head, "save");
					var ed = new publicjs.entitydata(head);
					var eo = new publicjs.entityobj();
					eo.AttrList = {
						Name: "UserInfo",
						DoType: this.mode,
						UseDistinct: false,
						OrgCode: "",
						UseBackUp: false,
						UseGroup: false,
					};
					//eo.ValueList = this.entityObj;
					eo.ValueList = changeList;
					if (this.mode == "Add") {
						for (let c = 0; c < this.addNum; c++)
							ed.Data.Content.ObjectList.Object.push(eo);
					} else ed.Data.Content.ObjectList.Object.push(eo);
					try {
						var returnValueStr =
							await this.$API.auth.objProcess.post(ed);
						console.log(returnValueStr);
						// eslint-disable-next-line no-debugger
						//debugger;
						//var returnValue = JSON.parse(returnValueStr);
						//alert(returnValue.opFlag);
						this.isSaveing = false;
						this.visible = false;
						this.$emit("fatherMethod");
					} catch (error) {
						this.loading = false;
						//this.emptyText = error.statusText;
						return false;
					}
				}
			});
		},
		//表单注入数据
		async setData(data) {
			//alert('aaa')
			// eslint-disable-next-line no-debugger
			debugger
			var head = new publicjs.head();
			publicjs.initHead(head, "getobj");
			// eslint-disable-next-line no-undef
			let ed = new publicjs.entitydata(head);
			var eo = new publicjs.entityobj();
			eo.AttrList = {
				Name: "UserInfo",
				UseDistinct: false,
				OrgCode: "",
				UseBackUp: false,
				UseGroup: false,
			};
			eo.ValueList = {
				Code: "Code",
				Name: "Name",
				// BrandCode:"BrandCode",
				OrgCode: "OrgCode",
				OrgName: "OrgName",
				DepartCode: "DepartCode",
				SexCode: "SexCode",
				DutyCode: "DutyCode",
				Mobile: "Mobile",
				Email: "Email",
				WeChat: "WeChat",
				BirthDate: "BirthDate",
				IdNumber: "IdNumber",
				IsCustomerMan: "IsCustomerMan",
				AllowOrder: "AllowOrder",
				Remark: "Remark",
				Attachment: "Attachment",
				State: "State",
				InLocal: "InLocal",
			};
			eo.QueryList = { Code: data.Code };
			ed.Data.Content.ObjectList.Object.push(eo);
			try {
				var returnValueStr = await this.$API.auth.objProcess.post(ed);
				console.log(returnValueStr);
				//eslint-disable-next-line no-debugger
				debugger;
				var returnValue = JSON.parse(returnValueStr);
				if (returnValue.Result.OpFlag == "1") {
					// eslint-disable-next-line no-debugger
					//debugger;
					let returnObj =
						returnValue.Result.Content.ObjectList.Object;
					this.entityObj.Code = this.entityObj.Code_Old =
						returnObj.ValueList.Code;
					// this.entityObj.BrandCode= this.entityObj.BrandCode_Old =
					// 	returnObj.ValueList.BrandCode;
					this.entityObj.Name = this.entityObj.Name_Old =
						returnObj.ValueList.Name;
					this.entityObj.OrgCode = this.entityObj.OrgCode_Old =
						returnObj.ValueList.OrgCode;
					this.entityObj.OrgName = this.entityObj.OrgName_Old =
						returnObj.ValueList.OrgName;
					this.entityObj.DepartCode = this.entityObj.DepartCode_Old =
						returnObj.ValueList.DepartCode;
					this.entityObj.SexCode = this.entityObj.SexCode_Old =
						returnObj.ValueList.SexCode;
					this.entityObj.DutyCode = this.entityObj.DutyCode_Old =
						returnObj.ValueList.DutyCode;
					this.entityObj.Mobile = this.entityObj.Mobile_Old =
						returnObj.ValueList.Mobile;
					this.entityObj.Email = this.entityObj.Email_Old =
						returnObj.ValueList.Email;
					this.entityObj.WeChat = this.entityObj.WeChat_Old =
						returnObj.ValueList.WeChat;
					this.entityObj.BirthDate = this.entityObj.BirthDate_Old =
						returnObj.ValueList.BirthDate;
					this.entityObj.IdNumber = this.entityObj.IdNumber_Old =
						returnObj.ValueList.IdNumber;
					this.entityObj.IsCustomerMan =
						this.entityObj.IsCustomerMan_Old =
							returnObj.ValueList.IsCustomerMan;
					this.entityObj.AllowOrder = this.entityObj.AllowOrder_Old =
						returnObj.ValueList.AllowOrder;
					this.entityObj.Remark = this.entityObj.Remark_Old =
						returnObj.ValueList.Remark;
					this.entityObj.Attachment = this.entityObj.Attachment_Old =
						returnObj.ValueList.Attachment;
					this.entityObj.State = this.entityObj.State_Old =
						returnObj.ValueList.State;
					this.entityObj.InLocal = "0";

					ed = null;
					eo = null;
					head = null;
				}
				//this.summary = response.summary || {};
				this.loading = false;
				//this.$refs.scTable.setScrollTop(0);
				//this.$emit('dataChange', res, this.tableData)
			} catch (error) {
				this.loading = false;
				//this.emptyText = error.statusText;
				return false;
			}
		},

		async initOptions() {
			var head = new publicjs.head();
			publicjs.initHead(head, "getfirstdatasource");
			var ed = new publicjs.entitydata(head);
			var eo = new publicjs.entityobj();
			eo.AttrList = {
				Name: "UserInfo",
				AcceptControl: "DepartCode,SexCode,DutyCode",
				OrgCode: "",
			};
			ed.Data.Content.ObjectList.Object.push(eo);
			try {
				var returnValueStr = await this.$API.auth.objProcess.post(ed);
				var returnValue = JSON.parse(returnValueStr);
				if (returnValue.Result.OpFlag == "1") {
					var objectList =
						returnValue.Result.Content.ObjectList.Object;
					for (let i = 0; i < objectList.length; i++) {
						let object = objectList[i];
						if (object.Name == "OrgCode") {
							this.optionsOrgCode = eval("(" + object.rows + ")");
						} else if (object.Name == "DepartCode") {
							this.optionsDepartCode = object.rows.row;
						} else if (object.Name == "SexCode") {
							this.optionsSexCode = object.rows.row;
						} else if (object.Name == "DutyCode") {
							this.optionsDutyCode = object.rows.row;
						} 
						// else if (object.Name == "BrandCode") {
						// 	this.optionsBrandCode = object.rows.row;
						// }
					}
				}
			} catch (error) {
				this.loading = false;
				this.$alert(error.message, "提示", { type: "error" });
				return false;
			}
		},
	},
	async mounted() {
		await this.initOptions();
	},
};
</script>

<style></style>
